package com.wgke.bluetooth.ui.fragment;

import android.graphics.PointF;
import android.opengl.GLSurfaceView;
import android.view.View;

import com.wgke.bluetooth.R;
import com.wgke.bluetooth.utils.LogUtil;
import com.wgke.bluetooth.utils.ToastUtil;
import com.wgke.bluetooth.view.PressureRenderer;
import com.wgke.bluetooth.vm.BaseVM;

public class HeatMap3DFragment extends BaseFragment {

    private GLSurfaceView glSurfaceView;
    private PressureRenderer renderer;
    private static final String TAG = "PressureVisualizationActivity";
    float strenth = 0.1f;

    @Override
    public int getLayoutId() {
        return R.layout.activity_chart;
    }

    @Override
    protected void initView(View rootView) {
        glSurfaceView = rootView.findViewById(R.id.glSurfaceView);
        glSurfaceView.setEGLContextClientVersion(2);
        // 创建渲染器
        renderer = new PressureRenderer();
        glSurfaceView.setRenderer(renderer);
        // 设置渲染模式为按需渲染，提高性能
        glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
        rootView.findViewById(R.id.toggleViewButton).setOnClickListener(v -> {
            if (strenth == 1.0f) {
                strenth = 0.1f;
            } else {
                strenth = 1f;
            }
            renderer.set3DEffectStrength(strenth);
        });

        rootView.findViewById(R.id.toggleViewButton2).setOnClickListener(v -> {
            startAnimation();
        });

        BaseVM vm = new BaseVM();
        vm.createBtn(rootView.findViewById(R.id.layout_total1), "角度：", 1.0f, 0.1f, v -> {
            float value = (Float) v.getTag();
            LogUtil.d(TAG, "角度：" + value);
            renderer.m3DEffectStrength = value;
        });

        vm.createBtn(rootView.findViewById(R.id.layout_total2), "视野：", 50.0f, 2f, v -> {
            float value = (Float) v.getTag();
            LogUtil.d(TAG, "视野：" + value);
            renderer.FOV_3D = value;
        });

        vm.createBtn(rootView.findViewById(R.id.layout_total3), "近平面：", 0.5f, 0.1f, v -> {
            float value = (Float) v.getTag();
            LogUtil.d(TAG, "视野：" + value);
            renderer.FOV_3D = value;
        });
        vm.createBtn(rootView.findViewById(R.id.layout_total3), "相机：", 2.5f, 0.1f, v -> {
            float value = (Float) v.getTag();
            LogUtil.d(TAG, "相机：" + value);
            renderer.CAMERA_Z = value;
        });
        vm.createBtn(rootView.findViewById(R.id.layout_total4), "PLANE：", 20.0f, 1f, v -> {
            float value = (Float) v.getTag();
            LogUtil.d(TAG, "PLANE：" + value);
            renderer.FAR_PLANE = value;
        });
        vm.createBtn(rootView.findViewById(R.id.layout_total5), "增高：", 0.6f, 0.1f, v -> {
            float value = (Float) v.getTag();
            LogUtil.d(TAG, "增高：" + value);
            renderer.mTarget3DEffectStrength = value;
        });
        vm.createBtn(rootView.findViewById(R.id.layout_total6), "着色点：", 5.0f, 0.5f, v -> {
            float value = (Float) v.getTag();
            LogUtil.d(TAG, "着色点：" + value);
            renderer.mPointSize = value;//0.05-0.3
        });
    }

    Thread thread = null;
    int count = 0;

    void startAnimation() {
        if (thread == null) {
            thread = new Thread(() -> {
                while (true) {
                    if (getActivity().isFinishing()){
                        return;
                    }
                    try {
                        Thread.sleep(30L);
                        PointF point = new PointF(50f, 33f); // 圆心 (50, 33)
                        float radius = 15f; // 半径15单位
                        float angleDegrees = count * 1f; // 角度5度
                        PointF pointOnCircle = getPointOnCircle(point, radius, angleDegrees);
                        getActivity().runOnUiThread(() -> renderer.initTestData((int) pointOnCircle.x, (int) pointOnCircle.y));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count++;
                }
            });
            thread.start();
        }
    }

    public PointF getPointOnCircle(PointF center, float radius, float angleDegrees) {
        float angleRadians = (float) Math.toRadians(angleDegrees);
        float x = center.x + radius * (float) Math.cos(angleRadians);
        float y = center.y + radius * (float) Math.sin(angleRadians);
        return new PointF(x, y);
    }

}

